﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WorkItemAnalyser.Core.Assertions;
using WorkItemAnalyser.Core.Parsers;

namespace WorkItemAnalyser.Test.ParserTests
{
    [TestClass]
    public class TextScenarioParserTests
    {

        [TestMethod]
        public void TextScenarioParser_ParseScenario_ShouldReturnNullOnNullText()
        {
            //Arrrange
            var parser = new TextScenarioParser();

            //Act
            var scenario = parser.ParseScenario(null);

            //Assert
            Assert.IsNull(scenario);
        }

        [TestMethod]
        public void TextScenarioParser_ParseScenario_ShouldReturnNullOnEmptyString()
        {
            //Arrrange
            var parser = new TextScenarioParser();

            //Act
            var scenario = parser.ParseScenario(string.Empty);

            //Assert
            Assert.IsNull(scenario);
        }

        [TestMethod]
        public void TextScenarioParser_ParseScenario_ShouldReturnNullOnInvalidString()
        {
            //Arrrange
            var parser = new TextScenarioParser();

            //Act
            var scenario = parser.ParseScenario("A random string of text");

            //Assert
            Assert.IsNull(scenario);
        }

        [TestMethod]
        public void TextScenarioParser_ParseScenario_ShouldReturnScenarioForBasicScenarioDescription()
        {
            //Arrrange
            var parser = new TextScenarioParser();

            //Act
            var scenario = parser.ParseScenario(@"for each Bug in Readify" + Environment.NewLine + 
                                                                        @"where the Iteration Path field is Readify\Iteration 1" + Environment.NewLine + 
                                                                        @"the Status field should be Closed");

            //Assert
            Assert.IsNotNull(scenario);
        }

        [TestMethod]
        public void TextScenarioParser_ParseScenario_ShouldReturnScenarioWithCorrectProjectAndTypeForBasicScenarioDescription()
        {
            //Arrrange
            var parser = new TextScenarioParser();

            //Act
            var scenario = parser.ParseScenario(@"for each Bug in Readify" + Environment.NewLine +
                                                                        @"where the Iteration Path field is Readify\Iteration 1" + Environment.NewLine +
                                                                        @"the Status field should be Closed");

            //Assert
            Assert.IsNotNull(scenario);
            Assert.AreEqual("Readify", scenario.TeamProjectName);
            Assert.AreEqual("Bug", scenario.WorkItemType);
        }

        [TestMethod]
        public void TextScenarioParser_ParseScenario_ShouldReturnScenarioWithCorrectFilterCountForSingleBasicScenarioDescription()
        {
            //Arrrange
            var parser = new TextScenarioParser();

            //Act
            var scenario = parser.ParseScenario(@"for each Bug in Readify" + Environment.NewLine +
                                                                        @"where the Iteration Path field is Readify\Iteration 1" + Environment.NewLine +
                                                                        @"the Status field should be Closed");

            //Assert
            Assert.IsNotNull(scenario);
            Assert.AreEqual(1, scenario.FilterAssertions.Count());
        }

        [TestMethod]
        public void TextScenarioParser_ParseScenario_ShouldReturnScenarioWithCorrectFilterCountForTwoFilterScenarioDescription()
        {
            //Arrrange
            var parser = new TextScenarioParser();

            //Act
            var scenario = parser.ParseScenario(@"for each Bug in Readify" + Environment.NewLine +
                                                                        @"where the Iteration Path field is Readify\Iteration 1" + Environment.NewLine +
                                                                        @"and the Iteration Path field is Readify\Iteration 1" + Environment.NewLine +
                                                                        @"the Status field should be Closed");

            //Assert
            Assert.IsNotNull(scenario);
            Assert.AreEqual(2, scenario.FilterAssertions.Count());
        }

        [TestMethod]
        public void TextScenarioParser_ParseScenario_ShouldReturnScenarioWithCorrectFilter()
        {
            //Arrrange
            var parser = new TextScenarioParser();

            //Act
            var scenario = parser.ParseScenario(@"for each Bug in Readify" + Environment.NewLine +
                                                                        @"where the Iteration Path field is Readify\Iteration 1" + Environment.NewLine +
                                                                        @"the Status field should be Closed");

            var filter = (FieldAssertion) scenario.FilterAssertions.FirstOrDefault();

            //Assert
            Assert.IsNotNull(scenario);
            Assert.AreEqual("Iteration Path", filter.FieldName);
            Assert.AreEqual(@"Readify\Iteration 1", filter.FieldValue);
        }

    }
}
